﻿#include "TestgRpcServer.h"

//need grpc.pb.h 
//#include "helloworld.grpc.pb.h"

//using namespace std;
//need use class and method
using helloworld::Greeter;
using helloworld::HelloReply;
using helloworld::HelloRequest;

// Logic and data behind the server's behavior.

/*
class GreeterServiceImpl final : public Greeter::Service {
  Status SayHello(ServerContext* context, const HelloRequest* request,
                  HelloReply* reply) override {
    std::string prefix("Hello ");
    reply->set_message(prefix + request->name());
    return Status::OK;
  }
};
*/

//服务端同步类方法的函数实现.
Status GreeterServiceImpl::SayHello(ServerContext* context, const HelloRequest* request,
                            HelloReply* reply)
{
      std::string prefix("Hello ");
      reply->set_message(prefix + request->name());
      return Status::OK;
}

#if 0
ServerImpl::ServerImpl()
{
    //已经是成员变量，不是指针，无需分配空间。
    //service_ = new Greeter::AsyncService();
    //cq_ = new std::unique_ptr<grpc::ServerCompletionQueue>();
}
//服务端异步类方法的函数实现.
ServerImpl::~ServerImpl()
{
    //server_->Shutdown();
    // Always shutdown the completion queue after the server.
    //cq_->Shutdown();
    //delete service_;
    //service_ = 0;
    //delete cq_;
    //cq_ = 0;
}
#endif

//业务处理，需要客户自定义实现
void ServerImpl::HandleRpcs()
{
    // Spawn a new CallData instance to serve new clients.
    new CallData(&service_, cq_.get());
    void* tag;  // uniquely identifies a request.
    bool ok;
    while (true) {
      // Block waiting to read the next event from the completion queue. The
      // event is uniquely identified by its tag, which in this case is the
      // memory address of a CallData instance.
      // The return value of Next should always be checked. This return value
      // tells us whether there is any kind of event or cq_ is shutting down.
      GPR_ASSERT(cq_->Next(&tag, &ok));
      GPR_ASSERT(ok);
      static_cast<CallData*>(tag)->Proceed();
    }

}
//回调的处理
void ServerImpl::CallData::Proceed()
{
    if (status_ == CREATE) {
      status_ = PROCESS;
      service_->RequestSayHello(&ctx_, &request_, &responder_, cq_, cq_, this);
    } else if (status_ == PROCESS) {
      new CallData(service_, cq_);

      // The actual processing.
      std::string prefix("Hello ");
      reply_.set_message(prefix + request_.name());

      status_ = FINISH;
      responder_.Finish(reply_, Status::OK, this);
    } else {
      GPR_ASSERT(status_ == FINISH);
      delete this;
    }
}

/*
 * 赋值类函数不好声明；
ServerImpl::CallData::CallData(Greeter::AsyncService* service, ServerCompletionQueue* cq)
    : service_(service), cq_(cq), responder_(&ctx_), status_(CREATE)
{
    // Invoke the serving logic right away.
    Proceed();
}
*/
//测试类方法的实现

TestGRPCServer * TestGRPCServer::m_pInstance = NULL;
TestGRPCServer * TestGRPCServer::Instance()
{
    if(m_pInstance == NULL) m_pInstance = new TestGRPCServer();
    return m_pInstance;
}

TestGRPCServer::TestGRPCServer()
{
    m_pInstance = 0;
    //service = 0;
    //server = new ServerImpl();
}
TestGRPCServer::~TestGRPCServer()
{
    delete m_pInstance;
    m_pInstance = 0;
    //delete service;
    //service = 0;
    //delete server;
    //server = 0;
}

//TestgRPC同步服务端的调用，传递获取的GRPC的接口和框架处理后的端口IP信息，使用自定义的服务类，依次启动。
void OSGI_API TestGRPCServer::TestgRPCSynServerRun(IGRPCService * pGRPCService, std::string server_address, bool enablepoll, int min_poll, int max_poll)
{
    char * address = const_cast<char*>(server_address.c_str());
    printf("address is [%s]\n",address);
    //service = new GreeterServiceImpl();
    pGRPCService->gRPCSynServerInit(address, enablepoll, min_poll, max_poll);
    //printf("666666666666\n");
    pGRPCService->gRPCSynServerRegister(&service);
    //printf("77777777777\n");
    pGRPCService->gRPCSynServerstart();
    //printf("88888888888\n");
}


//实际异步服务的启动,在插件实例化以后调用，实际的运行封装在测试函数内部
//TestgRPC异步服务端的调用
//gRPC异步服务的注册，调用之后需要自己手动处理业务
#if 0
int OSGI_API TestGRPCServer::TestgRPCAsynServerRun(IGRPCService * pGRPCService,
                                   std::string server_address,
                                   std::unique_ptr<ServerCompletionQueue> cq_,
                                   std::unique_ptr<Server> server_,
                                   grpc::Service service_)
#endif
void OSGI_API TestGRPCServer::TestgRPCAsynServerRun(IGRPCService * pGRPCService, std::string server_address)
{
    char * address = const_cast<char*>(server_address.c_str());
    pGRPCService->gRPCAsynServerInit(address);
    //printf("66666666666\n");
    //pGRPCService->gRPCAsynServerRegister(std::unique_ptr<ServerCompletionQueue>&(server.service_)); //Greeter::AsyncService
    pGRPCService->gRPCAsynServerRegister(server.cq_, &(server.service_));
    //printf("77777777777\n");
    pGRPCService->gRPCAsynServerstart(); //std::unique_ptr<Server>
    //printf("88888888888\n");
    // Proceed to the server's main loop. added by users.
    server.HandleRpcs();// Proceed to the server's main loop.

    //if need , Destory();
    //pGRPCService.gRPCAsynServerDestory(server.cq_);
}



